﻿
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<style type="text/css">
<!--

#edit
{
    opacity: 0.8;
}   
#edit:hover
{
    opacity: 1;
}
#del
{
    opacity: 0.8;
}
#del:hover
{
    opacity: 1;
}   
#check
{
    opacity: 0.8;
}
#check:hover
{
    opacity: 1;
}
    
-->
</style>
<script type="text/javascript" src="ckeditor/ckeditor.js"></script>
	<script src="ckeditor/_samples/sample.js" type="text/javascript"></script>
	<link href="ckeditor/_samples/sample.css" rel="stylesheet" type="text/css" />
</head>
<body>
<?php
		include_once("code/DataProvider.php");
		$query = "SELECT sodong FROM giaodien";
		$result = DataProvider::ExecuteQuery($query);
		$row = mysql_fetch_array($result);
		$rowsPerPage = $row['sodong'];
        if(!isset($_GET['page'])) //nếu không tồn tại page thì mặc nhiên sẽ là trang 1
		{						//vd: localhost/index.php = localhost/index.php?page=1
			$_GET['page'] = 1;
		}
		$offset = ($_GET['page']-1) * $rowsPerPage; 
		?>

      <?php
				$query="SELECT sp.MaSP, sp.TenSP, nd.TenDangNhap, sp.SoLuongTon, sp.SoLuongBan, hsx.TenHSX, l.TenLSP, sp.GiaSP, sp.HinhURL, sp.MoTaSP, sp.BiXoa, sp.NgayDang
                FROM sanpham sp, nguoidung nd, hangsx hsx, loaisp l
                WHERE sp.MaND = nd.MaND AND sp.MaLSP = l.MaLSP AND sp.MaHSX = hsx.MaHSX 
                ORDER BY NGAYDANG DESC";
				
				$query .= " LIMIT $offset, $rowsPerPage";
				
				$result=DataProvider::executeQuery($query);
				if($result)
				{
        ?>
          
            <h2 style="color: blue; text-align: center;">DANH SÁCH SẢN PHẨM</h2>
            <form action="#" name="search_sp" method="GET"  >
                    <i><u>&nbsp;&nbsp;Tìm Kiếm Nhanh&nbsp;&nbsp;</u></i><br />
                    <input type="text" name="txtSearchSP" class="search_input"  />
                    <a href = "pSearchSP.php"><input type="image" class="search_submit" src="images/search.png" /></a>
        
                <br />
                <br />
                &nbsp;Tên sản phẩm<input type="radio" name="search" id="search" value="1" checked="checked" /> |
                &nbsp;&nbsp;Giá<input type="radio" name="search" id="search" value="2"  /> |
                &nbsp;&nbsp;Tên người bán<input type="radio" name="search" id="search" value="3"  /> |
                &nbsp;&nbsp;Mô tả<input type="radio" name="search" id="search" value="4"  />
            </form> 
            <hr /><br />    
    <table border="1" cellspacing="0" style=" text-align: center; font-size: 10px;">
    	<tr style=" color: white; background: #555;">
            <td align="center" height="30px" width="50">Tên SP</td>
            <td align="center" height="30px" width="65">Người bán</td>
            <td align="center" height="30px" width="25">SL tồn</td>
            <td align="center" height="30px" width="25">SL bán</td>
            <td align="center" height="30px" width="40">HSX</td>
            <td align="center" height="30px" width="40">Loại SP</td>
            <td align="center" height="30px" width="50">Giá</td>
            <td align="center" height="30px" width="65">Hình</td>
            <td align="center" height="30px" width="200">Mô tả</td>
            <td align="center" height="30px" width="25">Bị xóa</td>
            <td align="center" height="30px" width="45">Ngày đăng</td>
            <td align="center" height="30px" width="35">Sửa</td>
            <td align="center" height="30px" width="35">Xóa</td>
        </tr><?php
        while ($row=mysql_fetch_array($result))
			{?>
        <tr>
            <td align="center" ><?php echo $row['TenSP'] ?></td>
            <td align="center" ><?php echo $row['TenDangNhap'] ?></td>
            <td align="center" ><?php echo $row['SoLuongTon'] ?></td>
            <td align="center" ><?php echo $row['SoLuongBan'] ?></td>
            <td align="center" ><?php echo $row['TenHSX'] ?></td>
            <td align="center" ><?php echo $row['TenLSP'] ?></td>
            <td align="center" ><?php echo $row['GiaSP'] ?> VNĐ</td>
            <td align="center" ><?php echo $row['HinhURL'] ?></td>
            <td align="center" ><?php echo $row['MoTaSP'] ?></td>
            <td align="center" ><?php echo $row['BiXoa'] ?></td>
            <td align="center" ><?php echo $row['NgayDang'] ?></td>
            <td align="center" >
                <a href="admin.php?act=10&id=<?php echo $row['MaSP']; ?>">
                    <img id="edit" src="images/edit-icon.png" height="25" width="25"/>   
                </a>
            </td>
            <td align="center">
                <a href="admin.php?act=13=&id=<?php echo $row['MaSP']; ?>">
                <?php
                    if($row['BiXoa'] == 1)
                        echo '<img id="check" src="images/check-icon.png" height="25" width="25"/>';
                    else
                        echo '<img id="del" src="images/delete-icon.png" height="25" width="25"/>';

                ?>  
                </a>
            </td>
        </tr><?php } ?>
	</table>	<?php }
				
		  ?>     </td>
  </tr>
</table>
<?php 
	//tinh tong so trang
	$sql="SELECT COUNT(sp.MaSP) AS numrows
                FROM sanpham sp, nguoidung nd, hangsx hsx, loaisp l
                WHERE sp.MaND = nd.MaND AND sp.MaLSP = l.MaLSP AND sp.MaHSX = hsx.MaHSX 
                ORDER BY NGAYDANG DESC";
	$result = DataProvider::ExecuteQuery($sql) ;
	$row = mysql_fetch_array($result, MYSQL_ASSOC);
	$n = $row['numrows'];
	
	$tong_so_trang = ceil($n/$rowsPerPage);
	$self = $_SERVER['PHP_SELF'];//lay ten file chua doan code do (file dang xem)
								//$PHP_SELF và $_SERVER['PHP_SELF'] là hoàn toàn giống nhau	
	//gan nut BACK
	if($_GET['page'] > 1)
	echo "<a href=".$self."?act=9"."&page=".($_GET['page']-1).' style="background-image:url(./images/bg.jpg); color: black;"> Back'."</a>";
	//tao cac lien ket tuong ung voi cac trang 
	if($tong_so_trang > 1){ //neu chi co 1 page chua san pham thi ko can phan trang
	for($i=1 ; $i<=$tong_so_trang; $i++)
	{
		if($i == $_GET['page'])
		{
			echo ' ['.$i.']'; //in page hien tai, ko can tao lien ket
		}
		else

		echo "<a href=".$self."?act=9"."&page=".$i.' style="color: #0661c2;"> '.$i."</a>";
	}
	}
	//gan nut NEXT
	if($_GET['page'] < $tong_so_trang)
	echo "<a href=".$self."?act=9"."&page=".($_GET['page']+1).' style="background-image:url(./images/bg.jpg); color: black;"> Next'."</a>";
?>

</body>
</html>

